import { Vector4, Matrix4, FrontSide, Material, MaterialParameters, LineBasicMaterial, LineDashedMaterial, MeshBasicMaterial, MeshDepthMaterial, MeshLambertMaterial, MeshNormalMaterial, MeshPhongMaterial, MeshPhysicalMaterial, MeshStandardMaterial, MeshToonMaterial, PointsMaterial, Object3D, Mesh, MathUtils, Vector3, Texture, Vector2 } from "three";
import MaterialShaderManager from "./MaterialShaderManager";
import { CustomMeshBasicMaterial, CustomMeshPhongMaterial, CustomMeshLambertMaterial, CustomMeshPhysicalMaterial, CustomMeshStandardMaterial, CustomLineBasicMaterial, CustomLineDashedMaterial, CustomMeshDepthMaterial, CustomMeshNormalMaterial, CustomMeshToonMaterial, CustomPointsMaterial } from "./CustomMaterial";
import Helper from "../../utils/Helper";


export default class MaterialAdapter {
    private material: Material;
    public shaderUniform;
    private clapData: { isOpenClap: boolean, clapPointArray: [], clapPointCount: number, renderSide };
    private static viewFieldData: { isOpenViewField: boolean, frustumPlanes: Array<Vector4>, frustumPlaneCount: number, viewFieldColor: Vector3, viewFieldDepthTexture?: Texture, viewMatrix_ViewField?: Matrix4, projectMatrix_ViewField?: Matrix4, isCoverShadow_ViewField?: boolean, shadowColor_ViewField?: Vector3 };
    private bloomData: { isOpenBloom: boolean, bloomPointArray: Array<number>, bloomPointCount: number, bloomHeight: number };
    private clapProjectData: { isOpenClapProject: boolean, clapProjectMatrix: Matrix4 }; //之前fbo渲染中用到的view-project矩阵
    private textureProjectData: { isOpenTextureProject: boolean, isCloned: boolean, virturalProjectMatrix: Matrix4, virturalViewMatrix: Matrix4, projectTexture: Texture, depthTexture: Texture, textureProjectMix: number, isCoverShadow: boolean };
    private projectDistortionData: { isOpenDistortion: boolean, distortionStep: number, distortionAngle: number }; //只在纹理投影的shader中使用反畸变的功能
    private projectMaskData: { isOpenProjectMask: boolean, projectMaskTexture: Texture }; //只在纹理投影的shader中使用遮罩的功能
    private floorDivisionData: { floorRangeArray: Array<Vector2>, floorColorArray: Array<Vector3>, isOpenFloorDivision: boolean }; //建筑楼层分层的数据
    private contourLineData: {isOpenContourLine: boolean, contourLineWidth: number, contourLineColor: Vector3, contourLineInterval: number}; //等高线的数据
    private static circleRangeData: {circelCenter: Vector3, circleRadius: number, circleBorderWidth: number, isOpenCircleRange: boolean, circleRangeColor: Vector3, circleBorderColor: Vector3, circleRangeMix: number}; //圆形范围的数据
    private static InitMatrix: Matrix4 = new Matrix4();

    //把一个object3D挂载的所有mesh的材质都替换成自定义材质
    public static convertObj(object, macrosList?: Array<string>, identity?: string) {
        if (object instanceof Mesh || object.type == 'Mesh') {
            if (object.material.length > 1) {
                let newMaterialArray = [];
                object.material.forEach(element => {
                    let newMaterial = MaterialAdapter.convertMaterial(element, macrosList, identity);
                    newMaterialArray.push(newMaterial);
                });
                object.material = newMaterialArray;
            } else {
                let newMaterial = MaterialAdapter.convertMaterial(object.material, macrosList, identity);
                object.material = newMaterial;
            }
        }

        Helper.traverseObject(object, function (obj) {
            if (obj instanceof Mesh || obj.type == 'Mesh') {
                if (obj.material.length > 1) {
                    let newMaterialArray = [];
                    obj.material.forEach(element => {
                        let newMaterial = MaterialAdapter.convertMaterial(element, macrosList, identity);
                        newMaterialArray.push(newMaterial);
                    });
                    obj.material = newMaterialArray;
                } else {
                    let newMaterial = MaterialAdapter.convertMaterial(obj.material, macrosList, identity);
                    obj.material = newMaterial;
                }
            }
            return true;
        });
    }

    public static convertMaterial(oldMaterial: Material, macrosList?: Array<string>, identity?: string) {
        let coreParameter = {};
        Object.assign(coreParameter, oldMaterial);

        let newMaterial;
        let materialParam = { coreParameters: coreParameter, macrosList: macrosList, identity: identity };

        if (oldMaterial instanceof MeshBasicMaterial) {
            newMaterial = new CustomMeshBasicMaterial(materialParam);
        } else if (oldMaterial instanceof MeshPhongMaterial) {
            newMaterial = new CustomMeshPhongMaterial(materialParam);
        } else if (oldMaterial instanceof MeshLambertMaterial) {
            newMaterial = new CustomMeshLambertMaterial(materialParam);
        } else if (oldMaterial instanceof MeshPhysicalMaterial) {
            newMaterial = new CustomMeshPhysicalMaterial(materialParam);
        } else if (oldMaterial instanceof MeshStandardMaterial) {
            newMaterial = new CustomMeshStandardMaterial(materialParam);
        } else if (oldMaterial instanceof LineBasicMaterial) {
            newMaterial = new CustomLineBasicMaterial(materialParam);
        } else if (oldMaterial instanceof LineDashedMaterial) {
            newMaterial = new CustomLineDashedMaterial(materialParam);
        } else if (oldMaterial instanceof MeshDepthMaterial) {
            newMaterial = new CustomMeshDepthMaterial(materialParam);
        } else if (oldMaterial instanceof MeshNormalMaterial) {
            newMaterial = new CustomMeshNormalMaterial(materialParam);
        } else if (oldMaterial instanceof MeshToonMaterial) {
            newMaterial = new CustomMeshToonMaterial(materialParam);
        } else if (oldMaterial instanceof PointsMaterial) {
            newMaterial = new CustomPointsMaterial(materialParam);
        } else {
            console.log('未转换的材质类型：' + oldMaterial.type);
            return oldMaterial;
        }

        return newMaterial;
    }

    public constructor(material: Material) {
        this.material = material;
        this.shaderUniform = null;
        this.clapData = null;
        this.bloomData = null;
        this.clapProjectData = null;
    }

    public setCut(clapData: { isOpenClap: boolean, clapPointArray: [], clapPointCount: number, renderSide: number }) {
        if (!clapData) {
            return;
        }

        this.clapData = clapData;
        this.material.side = (this.clapData && this.clapData.renderSide) ? this.clapData.renderSide : FrontSide;

        if (this.shaderUniform && this.shaderUniform.isOpenCut != undefined) {
            this.shaderUniform.isOpenCut.value = clapData.isOpenClap;
            this.shaderUniform.cutPointArray.value = clapData.clapPointArray;
            this.shaderUniform.cutPointCount.value = clapData.clapPointCount;
            this.material.needsUpdate = true;
        }
    }

    public setVewField(viewFieldData: { isOpenViewField: boolean, frustumPlanes: Array<Vector4>, frustumPlaneCount: number, viewFieldColor: Vector3, viewFieldDepthTexture?: Texture, viewMatrix_ViewField?: Matrix4, projectMatrix_ViewField?: Matrix4, isCoverShadow_ViewField: boolean, shadowColor_ViewField: Vector3 }) {
        MaterialAdapter.viewFieldData = viewFieldData;
        if (!viewFieldData.frustumPlanes || viewFieldData.frustumPlanes.length == 0) {
            viewFieldData.frustumPlanes = this.getDefaultFrustumPlanes();
        }

        if (this.shaderUniform) {
            this.shaderUniform.isOpenViewField.value = viewFieldData.isOpenViewField;
            this.shaderUniform.frustumPlanes.value = viewFieldData.frustumPlanes;
            this.shaderUniform.frustumPlaneCount.value = viewFieldData.frustumPlaneCount;
            this.shaderUniform.viewFieldColor.value = viewFieldData.viewFieldColor;
            this.shaderUniform.isCoverShadow_ViewField.value = viewFieldData.isCoverShadow_ViewField;
            if (viewFieldData.shadowColor_ViewField) {
                this.shaderUniform.shadowColor_ViewField.value = viewFieldData.shadowColor_ViewField;
            }

            if (viewFieldData.viewFieldDepthTexture) {
                this.shaderUniform.viewFieldDepthTexture.value = viewFieldData.viewFieldDepthTexture;
            }
            if (viewFieldData.viewMatrix_ViewField) {
                this.shaderUniform.viewMatrix_ViewField.value = viewFieldData.viewMatrix_ViewField;
            }
            if (viewFieldData.projectMatrix_ViewField) {
                this.shaderUniform.projectMatrix_ViewField.value = viewFieldData.projectMatrix_ViewField;
            }

            this.material.needsUpdate = true;
        }
    }

    public setTextrueProject(textureProjectData: { isOpenTextureProject: boolean, isCloned: boolean, virturalProjectMatrix: Matrix4, virturalViewMatrix: Matrix4, projectTexture: Texture, depthTexture: Texture, textureProjectMix: number, isCoverShadow: boolean }) {
        this.textureProjectData = textureProjectData;

        if (this.shaderUniform) {
            this.shaderUniform.isOpenTextureProject.value = textureProjectData.isOpenTextureProject;
            this.shaderUniform.isCloned.value = textureProjectData.isCloned;
            this.shaderUniform.virturalProjectMatrix.value = textureProjectData.virturalProjectMatrix;
            this.shaderUniform.virturalViewMatrix.value = textureProjectData.virturalViewMatrix;
            this.shaderUniform.depthTexture.value = textureProjectData.depthTexture;
            this.shaderUniform.projectTexture.value = textureProjectData.projectTexture;
            this.shaderUniform.textureProjectMix.value = textureProjectData.textureProjectMix;
            this.shaderUniform.isCoverShadow.value = textureProjectData.isCoverShadow;

            this.material.needsUpdate = true;
        }
    }

    //设置反畸变的参数。反畸变的功能只在纹理投影的部分生效
    public setProjectDistortion(distortionData: { isOpenDistortion: boolean, distortionStep: number, distortionAngle: number }) {
        this.projectDistortionData = distortionData;

        if (this.shaderUniform) {
            if (this.shaderUniform.isOpenDistortion == undefined) {
                this.shaderUniform.isOpenDistortion = { value: undefined };
                this.shaderUniform.distortionStep = { value: undefined };
                this.shaderUniform.distortionAngle = { value: undefined };
            }
            this.shaderUniform.isOpenDistortion.value = distortionData.isOpenDistortion;
            this.shaderUniform.distortionStep.value = distortionData.distortionStep;
            this.shaderUniform.distortionAngle.value = distortionData.distortionAngle;

            this.material.needsUpdate = true;
        }
    }

    //只在纹理投影的shader中使用遮罩的功能
    public setProjectMask(projectMaskData: { isOpenProjectMask: boolean, projectMaskTexture: Texture }) {
        this.projectMaskData = projectMaskData;

        if (this.shaderUniform) {
            this.shaderUniform.isOpenProjectMask.value = projectMaskData.isOpenProjectMask;
            this.shaderUniform.projectMaskTexture.value = projectMaskData.projectMaskTexture;

            this.material.needsUpdate = true;
        }
    }

    public setBloom(bloomData: { isOpenBloom: boolean, bloomPointArray: Array<number>, bloomPointCount: number, bloomHeight: number }) {
        if (!bloomData) {
            return;
        }
        this.bloomData = bloomData;

        if (this.shaderUniform && this.shaderUniform.isOpenBloom != undefined) {
            this.shaderUniform.isOpenBloom.value = bloomData.isOpenBloom;
            this.shaderUniform.bloomPointArray.value = bloomData.bloomPointArray;
            this.shaderUniform.bloomPointCount.value = bloomData.bloomPointCount;
            this.shaderUniform.bloomHeight.value = bloomData.bloomHeight;
            this.material.needsUpdate = true;
        }
    }

    public setClapProjectData(clapProjectData: { isOpenClapProject: boolean, clapProjectMatrix: Matrix4 }) {
        if (!clapProjectData) {
            return;
        }

        this.clapProjectData = clapProjectData;

        if (this.shaderUniform && this.shaderUniform.isOpenClapProject != undefined) {
            this.shaderUniform.isOpenClapProject.value = clapProjectData.isOpenClapProject;
            this.shaderUniform.clapProjectMatrix.value = clapProjectData.clapProjectMatrix;
            this.material.needsUpdate = true;
        }
    }

    public setFloorDivisionData(floorDivisionData: { floorRangeArray: Array<Vector2>, floorColorArray: Array<Vector3>, isOpenFloorDivision: boolean }) {
        this.floorDivisionData = floorDivisionData;

        if (this.shaderUniform && this.shaderUniform.isOpenFloorDivision) {
            this.shaderUniform.isOpenFloorDivision.value = floorDivisionData.isOpenFloorDivision;
            this.shaderUniform.floorRangeArray.value = floorDivisionData.floorRangeArray;
            this.shaderUniform.floorColorArray.value = floorDivisionData.floorColorArray;

            this.material.needsUpdate = true;
        }
    }

    public setContourLineData(contourLineData: {isOpenContourLine: boolean, contourLineWidth: number, contourLineColor: Vector3, contourLineInterval: number}){
        this.contourLineData = contourLineData;

        if (this.shaderUniform && this.shaderUniform.isOpenContourLine) {
            this.shaderUniform.isOpenContourLine.value = contourLineData.isOpenContourLine;
            if(contourLineData.isOpenContourLine == true){
                this.shaderUniform.contourLineWidth.value = contourLineData.contourLineWidth;
                this.shaderUniform.contourLineColor.value = contourLineData.contourLineColor;
                this.shaderUniform.contourLineInterval.value = contourLineData.contourLineInterval;
            }

            this.material.needsUpdate = true;
        }
    }
    
    public setCircleRangeData(circleRangeData: {circelCenter: Vector3, circleRadius: number, circleBorderWidth: number, isOpenCircleRange: boolean, circleRangeColor: Vector3, circleBorderColor: Vector3, circleRangeMix: number}){
        MaterialAdapter.circleRangeData = circleRangeData;

        if(this.shaderUniform && circleRangeData){
            this.shaderUniform.circelCenter.value = circleRangeData.circelCenter;
            this.shaderUniform.circleRadius.value = circleRangeData.circleRadius;
            this.shaderUniform.circleBorderWidth.value = circleRangeData.circleBorderWidth;
            this.shaderUniform.isOpenCircleRange.value = circleRangeData.isOpenCircleRange;
            this.shaderUniform.circleRangeColor.value = circleRangeData.circleRangeColor;
            this.shaderUniform.circleBorderColor.value = circleRangeData.circleBorderColor;
            this.shaderUniform.circleRangeMix.value = circleRangeData.circleRangeMix;

            this.material.needsUpdate = true;
        }
    }

    private getDefaultFrustumPlanes() {
        let frustumPlanes: Array<Vector4> = new Array<Vector4>();
        let defaultPlane = new Vector4(0, 0, 0, 0);
        frustumPlanes.push(defaultPlane);
        frustumPlanes.push(defaultPlane);
        frustumPlanes.push(defaultPlane);
        frustumPlanes.push(defaultPlane);
        frustumPlanes.push(defaultPlane);
        frustumPlanes.push(defaultPlane);
        return frustumPlanes;
    }

    public preCompileShader(shader, materialParam: { coreParameters?: MaterialParameters, macrosList?: Array<string>, identity?: string }) {
        this.shaderUniform = shader.uniforms;

        shader.uniforms.isOpenCut = { value: (this.clapData && this.clapData.isOpenClap) ? this.clapData.isOpenClap : false };
        shader.uniforms.cutPointArray = { value: (this.clapData && this.clapData.clapPointArray) ? this.clapData.clapPointArray : [] };
        shader.uniforms.cutPointCount = { value: (this.clapData && this.clapData.clapPointCount) ? this.clapData.clapPointCount : 0 };

        shader.uniforms.isOpenBloom = { value: (this.bloomData && this.bloomData.isOpenBloom) ? this.bloomData.isOpenBloom : false };
        shader.uniforms.bloomPointArray = { value: (this.bloomData && this.bloomData.bloomPointArray) ? this.bloomData.bloomPointArray : [] };
        shader.uniforms.bloomPointCount = { value: (this.bloomData && this.bloomData.bloomPointCount) ? this.bloomData.bloomPointCount : 0 };
        shader.uniforms.bloomHeight = { value: (this.bloomData && this.bloomData.bloomHeight) ? this.bloomData.bloomHeight : 0 };

        shader.uniforms.isOpenClapProject = { value: (this.clapProjectData && this.clapProjectData.isOpenClapProject) ? this.clapProjectData.isOpenClapProject : false };
        shader.uniforms.clapProjectMatrix = { value: (this.clapProjectData && this.clapProjectData.clapProjectMatrix) ? this.clapProjectData.clapProjectMatrix : MaterialAdapter.InitMatrix };

        shader.uniforms.isOpenViewField = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.isOpenViewField) ? MaterialAdapter.viewFieldData.isOpenViewField : false };
        shader.uniforms.frustumPlanes = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.frustumPlanes) ? MaterialAdapter.viewFieldData.frustumPlanes : this.getDefaultFrustumPlanes() };
        shader.uniforms.frustumPlaneCount = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.frustumPlaneCount) ? MaterialAdapter.viewFieldData.frustumPlaneCount : 0 };
        shader.uniforms.viewFieldColor = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.viewFieldColor) ? MaterialAdapter.viewFieldData.viewFieldColor : new Vector3(1, 0, 0) };
        shader.uniforms.viewFieldDepthTexture = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.viewFieldDepthTexture) ? MaterialAdapter.viewFieldData.viewFieldDepthTexture : null };
        shader.uniforms.viewMatrix_ViewField = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.viewMatrix_ViewField) ? MaterialAdapter.viewFieldData.viewMatrix_ViewField : MaterialAdapter.InitMatrix };
        shader.uniforms.projectMatrix_ViewField = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.projectMatrix_ViewField) ? MaterialAdapter.viewFieldData.projectMatrix_ViewField : MaterialAdapter.InitMatrix };
        shader.uniforms.isCoverShadow_ViewField = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.isCoverShadow_ViewField) ? MaterialAdapter.viewFieldData.isCoverShadow_ViewField : false };
        shader.uniforms.shadowColor_ViewField = { value: (MaterialAdapter.viewFieldData && MaterialAdapter.viewFieldData.shadowColor_ViewField) ? MaterialAdapter.viewFieldData.shadowColor_ViewField : new Vector3(0.5, 0, 0) };

        shader.uniforms.isOpenTextureProject = { value: (this.textureProjectData && this.textureProjectData.isOpenTextureProject) ? this.textureProjectData.isOpenTextureProject : false };
        shader.uniforms.isCloned = { value: (this.textureProjectData && this.textureProjectData.isCloned) ? this.textureProjectData.isCloned : false };
        shader.uniforms.virturalProjectMatrix = { value: (this.textureProjectData && this.textureProjectData.virturalProjectMatrix) ? this.textureProjectData.virturalProjectMatrix : MaterialAdapter.InitMatrix };
        shader.uniforms.virturalViewMatrix = { value: (this.textureProjectData && this.textureProjectData.virturalViewMatrix) ? this.textureProjectData.virturalViewMatrix : MaterialAdapter.InitMatrix };
        shader.uniforms.depthTexture = { value: (this.textureProjectData && this.textureProjectData.depthTexture) ? this.textureProjectData.depthTexture : null };
        shader.uniforms.projectTexture = { value: (this.textureProjectData && this.textureProjectData.projectTexture) ? this.textureProjectData.projectTexture : null };
        shader.uniforms.textureProjectMix = { value: (this.textureProjectData && this.textureProjectData.textureProjectMix) ? this.textureProjectData.textureProjectMix : 0.5 };
        shader.uniforms.isCoverShadow = { value: (this.textureProjectData && this.textureProjectData.isCoverShadow) ? this.textureProjectData.isCoverShadow : true }; //是否开启阴影遮挡功能

        //设置反畸变的参数。反畸变的功能只在纹理投影的部分生效
        shader.uniforms.isOpenDistortion = { value: (this.projectDistortionData && this.projectDistortionData.isOpenDistortion) ? this.projectDistortionData.isOpenDistortion : false };
        shader.uniforms.distortionStep = { value: (this.projectDistortionData && this.projectDistortionData.distortionStep) ? this.projectDistortionData.distortionStep : 0 };
        shader.uniforms.distortionAngle = { value: (this.projectDistortionData && this.projectDistortionData.distortionAngle) ? this.projectDistortionData.distortionAngle : 0 };

        //设置遮罩的参数。遮罩的功能只在纹理投影的部分生效 isOpenProjectMask: boolean, projectMaskTexture:
        shader.uniforms.isOpenProjectMask = { value: (this.projectMaskData && this.projectMaskData.isOpenProjectMask) ? this.projectMaskData.isOpenProjectMask : false };
        shader.uniforms.projectMaskTexture = { value: (this.projectMaskData && this.projectMaskData.projectMaskTexture) ? this.projectMaskData.projectMaskTexture : null };

        //设置楼层分层的参数
        if (this.floorDivisionData && this.floorDivisionData.floorRangeArray && this.floorDivisionData.floorRangeArray.length > 0) {
            shader.uniforms.isOpenFloorDivision = { value: (this.floorDivisionData && this.floorDivisionData.isOpenFloorDivision) ? this.floorDivisionData.isOpenFloorDivision : null };
            shader.uniforms.floorRangeArray = { value: (this.floorDivisionData && this.floorDivisionData.floorRangeArray) ? this.floorDivisionData.floorRangeArray : null };
            shader.uniforms.floorColorArray = { value: (this.floorDivisionData && this.floorDivisionData.floorColorArray) ? this.floorDivisionData.floorColorArray : null };
        }

        //设置等高线的参数
        shader.uniforms.isOpenContourLine = { value: (this.contourLineData && this.contourLineData.isOpenContourLine) ? this.contourLineData.isOpenContourLine : false };
        shader.uniforms.contourLineColor = { value: (this.contourLineData && this.contourLineData.contourLineColor) ? this.contourLineData.contourLineColor :  new Vector3(1, 0, 0) };
        shader.uniforms.contourLineInterval = { value: (this.contourLineData && this.contourLineData.contourLineInterval) ? this.contourLineData.contourLineInterval : 0 };
        shader.uniforms.contourLineWidth = { value: (this.contourLineData && this.contourLineData.contourLineWidth) ? this.contourLineData.contourLineWidth : 0 };
        
        if(MaterialAdapter.circleRangeData){
            //设置圆形范围的参数
            shader.uniforms.circelCenter = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circelCenter) ? MaterialAdapter.circleRangeData.circelCenter : new Vector3(1, 0, 0) };
            shader.uniforms.circleRadius = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circleRadius) ? MaterialAdapter.circleRangeData.circleRadius : 0 };
            shader.uniforms.circleBorderWidth = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circleBorderWidth) ? MaterialAdapter.circleRangeData.circleBorderWidth : 0 };
            shader.uniforms.isOpenCircleRange = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.isOpenCircleRange) ? MaterialAdapter.circleRangeData.isOpenCircleRange : false };
            shader.uniforms.circleRangeColor = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circleRangeColor) ? MaterialAdapter.circleRangeData.circleRangeColor : new Vector3(1, 0, 0) };
            shader.uniforms.circleBorderColor = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circleBorderColor) ? MaterialAdapter.circleRangeData.circleBorderColor : new Vector3(1, 0, 0) };
            shader.uniforms.circleRangeMix = { value: (MaterialAdapter.circleRangeData && MaterialAdapter.circleRangeData.circleRangeMix) ? MaterialAdapter.circleRangeData.circleRangeMix : 0 };
        }
        
        let macrosList = (materialParam != null) ? materialParam.macrosList : null;
        //shader会被缓存，新的材质如果发现materialParam.coreParameters和缓存中的材质materialParam.coreParameters相同，就会使用缓存的shader，为了避免使用缓存，可以使用customProgramCacheKey
        shader.vertexShader = MaterialShaderManager.getBasicVertextShaderStr(shader.vertexShader, macrosList); // self.getVertexShader();
        shader.fragmentShader = MaterialShaderManager.getBasicFragemntShaderStr(shader.fragmentShader, macrosList); //self.getFragmentShader();
    }

    public setCustomKey(materialParam: { coreParameters?: MaterialParameters, macrosList?: Array<string>, identity?: string }) {
        if (materialParam && materialParam.identity) {
            this.material.customProgramCacheKey = () => {  //避免onBeforeCompile中的shader缓存
                return materialParam.identity;
            }
        }
    }
}